/*
 * 线路自动补全
 */


//增加线路下标
var lineIndex = 0;

//业务分类 0:城际专用；1：市内专用；-1：城际与市内均可用
var is_incity = -1;

// 获取起点城市
function getStartCity(startL, endL, layerIframe, selectStartValue, selectEndValue) {
    var param = JSON.stringify({"start_line_bm": ""});
    $.ajax({
        type: "POST",
        url: "/common/getstartlines",
        contentType: "application/json",
        dataType: "json",
        data: param,
        success: function (data) {
            if (data.status == 0) {
                var startCities = [];
                var defaultValue;
                for (var i = 0; i < data.startlines.length; i++) {
                    if (selectStartValue) {
                        if (data.startlines[i].line_bm == selectStartValue) {
                            //设置已选的起点城市
                            defaultValue = {
                                id: data.startlines[i].line_bm,
                                name: data.startlines[i].line_name
                            };
                        }
                    }
                    startCities[i] = {
                        id: data.startlines[i].line_bm,
                        name: data.startlines[i].line_name
                    };
                }
                //设置回调，获取相应的终点城市
                var itemSelectFuntion = function () {
                    getEndCity(endL, layerIframe, startL.attr('data'));
                };
                //构造下拉框
                $.selectSuggest(startL, startCities, filtStartCity, itemSelectFuntion, layerIframe, defaultValue);

                //设置已选的终点城市
                if (selectEndValue) {
                    getEndCity(endL, layerIframe, selectStartValue, selectEndValue);
                }
            } else if (data.status == -501) {//session过期
                window.location.href = '/timeout/index';
            } else {
                bbx_alert_error(data.message);
            }
        },
        error: function () {
            bbx_alert_error("出错啦~");
        }
    });
}

// 获取终点城市
function getEndCity(endL, layerIframe, startId, selectValue) {
    var param = JSON.stringify({"start_line_bm": startId});
    $.ajax({
        type: "POST",
        url: "/common/getendlines",
        contentType: "application/json",
        dataType: "json",
        data: param,
        success: function (data) {
            if (data.status == 0) {
                var endCities = [];
                var defaultValue;
                for (var i = 0; i < data.endlines.length; i++) {
                    if (selectValue) {
                        if (data.endlines[i].line_bm == selectValue) {
                            defaultValue = {
                                id: data.endlines[i].line_bm,
                                name: data.endlines[i].line_name
                            };
                        }
                    }
                    endCities[i] = {
                        id: data.endlines[i].line_bm,
                        name: data.endlines[i].line_name
                    };
                }
                $.selectSuggest(endL, endCities, '', '', layerIframe, defaultValue);
            } else if (data.status == -501) {//session过期
                window.location.href = '/timeout/index';
            } else {
                bbx_alert_error(data.message);
            }
        },
        error: function () {
            bbx_alert_error("出错啦~");
        }
    });
}

function _addLinesModel(self, elem, isIncrease, selectStartValue, selectEndValue) {
    var lineContent = ""
            + '<div class="line-model">'
            + '<div class="form-group">'
            + '<input class="form-control auto-complete" name="" id="startCitiesSelect' + lineIndex + '" type="text">'
            + '至<input class="form-control auto-complete" name="" id="endCitiesSelect' + lineIndex + '" type="text">'
            + '<div class="line-buttons">'
            + '<button class="decrease"><i class="iconfont">&#xe60a;</i></button>'
            + '<button class="increase"><i class="iconfont">&#xe607;</i></button>'
            + '</div>'
            + '</div>'
            + '</div>';
    if (isIncrease) {
        elem.find('.route-part-control').append(lineContent);
        self.hide().siblings('.decrease').show();
        getStartCity(elem.find('#startCitiesSelect' + lineIndex), elem.find('#endCitiesSelect' + lineIndex), elem, selectStartValue, selectEndValue);
        lineIndex++;
    } else {
        if (!self.siblings('.increase').is(':hidden')) {
            self.closest('.line-model').prev('.line-model').find('.increase').show();
        }
        self.closest('.line-model').remove();

        if (elem.find('.line-model').length == 1) {
            elem.find('.line-model .decrease').hide();
        }
    }

}

// 增加部分线路
function addLinesModel(elem) {
    // 线路选择点击增加
    elem.on('click', '.line-buttons .increase', function () {
        _addLinesModel($(this), elem, true);
    });
    // 线路选择点击减
    elem.on('click', '.line-buttons .decrease', function () {
        _addLinesModel($(this), elem, false);
    });
}

// 获取选择的线路
function routeSelect(elem) {
    var usableline = [];
    if (elem.find("#routeAll").attr("checked")) {
        usableline = new Array("*");
    } else {
        elem.find('.route-part-control .line-model').each(function () {
            var line = $(this).find('input:first-child').attr('data') + '_to_' + $(this).find('input:nth-child(2)').attr('data');
            var i = 0;
            for (; i < usableline.length; i++) {
                if (usableline[i] == line) {
                    break;
                }
            }
            if (i == usableline.length) {
                usableline.push(line);
            }
        });
    }
    return usableline;
}

//根据是否只是市内业务过滤可选择的起点城市
function filtStartCity(data) {
    var bussiness = '';
    switch (is_incity) {
        case 0:
            bussiness = '城际';
            break;
        case 1:
            bussiness = '市内';
            break;
        default:
            break;
    }
    if (!bussiness) {
        return data;
    }
    var newData = [];
    for (var i = 0; i < data.length; i++) {
        if (data[i].name.indexOf(bussiness) != -1) {
            newData.push(data[i]);
        }
    }
    return newData;
}